﻿/*
Author: Adarsh Kumar
Objective: Function to pull all ECs across all years with consistent vars
*/

/* Note: raw variable names have been changed in the code files to abide by Census disclosure guidelines */


*Set local macros for call to function
local ec = "`1'"
local ec_dir = "`2'"
local rcpt_var = "`3'" 
local cond = "`4'"
local source = "`5'"
local empl_var = "`6'" 
local pay_var = "`7'"

cd "${build_raw_dir}"

capture program drop id_clean 
program id_clean 
	
	*Cleaning ID vars 
	tostring cennum, replace 
	replace cennum = "" if cennum == "."
	tostring surnum, replace
	replace surnum = "" if surnum == "."
	gen cen_sur_num = CFN if YEAR <= 1997 
	replace cen_sur_num = surnum if YEAR > 1997
	
end

use "`ec_dir'/`ec'", clear
id_clean

*Drop if observations are suspect 
drop if `cond'

keep YEAR lbdid_v16 cen_sur_num `empl_var' `pay_var' `rcpt_var'
rename `rcpt_var' final_sales
rename `empl_var' final_emp 
rename `pay_var' final_salaries

gen source = "`source'"
local save_string = substr("`ec_dir'",4,3)
save "${build_raw_dir}/10_sales_data/`save_string'_sales.dta", replace 
